from direct.task import Task
from direct.actor import Actor
from pandac.PandaModules import *
from direct.interval.MetaInterval import *

from direct.gui.DirectGui import *
from direct.interval.IntervalGlobal import *
from direct.showbase import DirectObject



class CameraMover(DirectObject):

    def __init__(_):
        base.camera.setPos(0,-10,100)
        base.camera.setHpr(0,-35,0)
        _.camTask = taskMgr.add(_.move,'CameraMover')

        _.limit = (0.2, 0.2)
        _.zoomlimit = (50,150)
        _.rate = 0.3

        _.accept('wheel_up',_.zoomIn)
        _.accept('wheel_down',_.zoomOut)

    def zoomIn(_):
        if base.camera.getZ() < _.zoomlimit[0]:
            return        
        movement = LerpPosInterval(base.camera,0.3,base.camera.getPos()+Vec3(0,10,-10),camera.getPos())
        movement.start()
        #camera.setY(camera.getY()+1)
        #camera.setZ(camera.getZ()-1)

    def zoomOut(_):
        if base.camera.getZ() > _.zoomlimit[1]:
            return
        
        movement = LerpPosInterval(base.camera,0.3,base.camera.getPos()+Vec3(0,-10,10),camera.getPos())
        movement.start()
        #camera.setY(camera.getY()-1)
        #camera.setZ(camera.getZ()+1)


    def move(_, task):
        if not base.mouseWatcherNode.hasMouse():
            return Task.cont
            
        camx = base.camera.getX()
        camy = base.camera.getY()
        camz = base.camera.getZ()

        maus = base.mouseWatcherNode.getMouse()
        x = maus.getX()
        y = maus.getY()

        if x <= -1.0 + _.limit[0]:
            base.camera.setPos(camx-rate, camy, camz)
        if x >= 1.0 - _.limit[1]:
            base.camera.setPos(camx+rate, camy, camz)
        if y <= -1.0 + _.limit[0]:
            base.camera.setPos(camx, camy-rate, camz)
        if y >= 1.0 - _.limit[1]:
            base.camera.setPos(camx, camy+rate, camz)

        return Task.cont
